Passed
Push — develop ( 77c94a...be0d3a )
by Xaver
02:54
created

node.js ➔ ... ➔ showClients   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 27
c 1
b 0
f 0
nc 2
dl 0
loc 36
rs 9.232
nop 1
1
define(['snabbdom', 'helper', 'moment'], function (V, helper, moment) {
2
  'use strict';
3
  V = V.default;
4
5
  var self = {};
6
7
  function showBar(v, width, warning) {
8
    return V.h('span',
9
      { props: { className: 'bar' + (warning ? ' warning' : '') } },
10
      [
11
        V.h('span',
12
          {
13
            style: { width: (width * 100) + '%' }
14
          }),
15
        V.h('label', v)
16
      ]
17
    );
18
  }
19
20
  self.showStatus = function showStatus(d) {
21
    return V.h('td',
22
      { props: { className: d.is_online ? 'online' : 'offline' } },
23
      _.t((d.is_online ? 'node.lastOnline' : 'node.lastOffline'), {
24
        time: d.lastseen.fromNow(),
25
        date: d.lastseen.format('DD.MM.YYYY, H:mm:ss')
26
      }));
27
  };
28
29
  self.showGeoURI = function showGeoURI(d) {
30
    if (!helper.hasLocation(d)) {
31
      return undefined;
32
    }
33
34
    return V.h('td',
35
      V.h('a',
36
        { props: { href: 'geo:' + d.location.latitude + ',' + d.location.longitude } },
37
        Number(d.location.latitude.toFixed(6)) + ', ' + Number(d.location.longitude.toFixed(6))
38
      )
39
    );
40
  };
41
42
  self.showGateway = function showGateway(d) {
43
    return d.is_gateway ? _.t('yes') : undefined;
44
  };
45
46
  self.showFirmware = function showFirmware(d) {
47
    return [
48
      helper.dictGet(d, ['firmware', 'release']),
49
      helper.dictGet(d, ['firmware', 'base'])
50
    ].filter(function (n) {
51
      return n !== null;
52
    }).join(' / ') || undefined;
53
  };
54
55
  self.showUptime = function showUptime(d) {
56
    return moment.utc(d.uptime).local().fromNow(true);
57
  };
58
59
  self.showFirstSeen = function showFirstSeen(d) {
60
    return d.firstseen.fromNow(true);
61
  };
62
63
  self.showLoad = function showLoad(d) {
64
    return showBar(d.loadavg.toFixed(2), d.loadavg / (d.nproc || 1), d.loadavg >= d.nproc);
65
  };
66
67
  self.showRAM = function showRAM(d) {
68
    return showBar(Math.round(d.memory_usage * 100) + ' %', d.memory_usage, d.memory_usage >= 0.8);
69
  };
70
71
  self.showDomain = function showDomain(d) {
72
    var rt = d.domain;
73
    if (config.domainNames) {
74
      config.domainNames.forEach(function (t) {
75
        if (rt === t.domain) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if rt === t.domain is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
76
          return t.name;
77
        }
78
      });
79
    }
80
    return rt;
81
  };
82
83
  self.showClients = function showClients(d) {
84
    if (!d.is_online) {
85
      return undefined;
86
    }
87
88
    var clients = [
89
      V.h('span', [
90
        d.clients > 0 ? d.clients : _.t('none'),
91
        V.h('br'),
92
        V.h('i', { props: { className: 'ion-people', title: _.t('node.clients') } })
93
      ]),
94
      V.h('span',
95
        { props: { className: 'legend-24ghz' } },
96
        [
97
          d.clients_wifi24,
98
          V.h('br'),
99
          V.h('span', { props: { className: 'symbol', title: '2,4 Ghz' } })
100
        ]),
101
      V.h('span',
102
        { props: { className: 'legend-5ghz' } },
103
        [
104
          d.clients_wifi5,
105
          V.h('br'),
106
          V.h('span', { props: { className: 'symbol', title: '5 Ghz' } })
107
        ]),
108
      V.h('span',
109
        { props: { className: 'legend-others' } },
110
        [
111
          d.clients_other,
112
          V.h('br'),
113
          V.h('span', { props: { className: 'symbol', title: _.t('others') } })
114
        ])
115
    ];
116
117
    return V.h('td', { props: { className: 'clients' } }, clients);
118
  };
119
120
  self.showIPs = function showIPs(d) {
121
    var string = [];
122
    var ips = d.addresses;
123
    ips.sort();
124
    ips.forEach(function (ip, i) {
125
      if (i > 0) {
126
        string.push(V.h('br'));
127
      }
128
129
      if (ip.indexOf('fe80:') !== 0) {
130
        string.push(V.h('a', { props: { href: 'http://[' + ip + ']/', target: '_blank' } }, ip));
131
      } else {
132
        string.push(ip);
133
      }
134
    });
135
    return V.h('td', string);
136
  };
137
138
  self.showAutoupdate = function showAutoupdate(d) {
139
    return d.autoupdater.enabled ? _.t('node.activated', { branch: d.autoupdater.branch }) : _.t('node.deactivated');
140
  };
141
142
  return self;
143
});
144